package cc.yihy.storage.ftp.bean;


import cc.yihy.storage.StorageClient;
import cc.yihy.storage.entity.UpResult;
import cc.yihy.storage.ftp.FtpProperties;
import cc.yihy.storage.util.StorageUtil;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author Yihy
 * @version V1.0
 * @Package com.peoplecitic.news.management.config.impl.ftp
 * @Description:
 * @date 2017/7/27 17:43
 */
public class SimpleFtpClient implements StorageClient {

    private FtpProperties properties;


    private FtpClient ftpClient;

    public SimpleFtpClient(FtpProperties properties, FtpClient ftpClient) {
        this.properties = properties;
        this.ftpClient = ftpClient;
    }


    @Override
    public UpResult upload(String filePath, String fileName) throws IOException {

        ftpClient = new FtpClient(properties.getHost(),
                properties.getPort(),
                properties.getUsername(),
                properties.getPassword(),
                properties.getRemoteDicrory());
        boolean upload = ftpClient.upload(fileName, filePath);
        ftpClient.destroy();
        UpResult result = new UpResult();
        result.setSuccess(upload);
        result.setFileName(fileName);
        return result;
    }

    @Override
    public UpResult upload(InputStream inputStream, String fileName) {
        ftpClient = new FtpClient(properties.getHost(),
                properties.getPort(),
                properties.getUsername(),
                properties.getPassword(),
                properties.getRemoteDicrory());
        boolean upload = ftpClient.upload(fileName, inputStream);
        UpResult result = new UpResult();
        result.setSuccess(upload);
        result.setFileName(fileName);
        return result;
    }

    @Override
    public String getDomain() {
        return properties.getDomain();
    }

    @Override
    public String getImageUrl(String fileName) {
        return StorageUtil.filePath(properties.getDomain(),fileName);
    }

    @Override
    public boolean delete(String fileKey) {

        return ftpClient.remove(fileKey);
    }
}
